package com.scommunity.admin.controller;

import com.github.pagehelper.PageInfo;
import com.scommunity.common.ResponseResult;
import com.scommunity.domain.Car;
import com.scommunity.domain.User;
import com.scommunity.service.IBuildingService;
import com.scommunity.service.ICarService;
import com.scommunity.utils.SnowflakeIdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/admin/car")
public class CarController{

    @Autowired
    private ICarService iCarService;

    @Autowired
    private SnowflakeIdWorker snowflakeIdWorker;
    @PostMapping("/add")
    public Object add(@RequestBody Car car){
        long carId = snowflakeIdWorker.nextId();
        car.setCarId(carId);
        Car ncar = iCarService.add(car);
        return ResponseResult.success(ncar);
    }

    @PostMapping("/time")
    public void addTime(@RequestBody Car car){
        Date d = new Date();
        SimpleDateFormat sbf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = sbf.format(d);
        if(car.getOutTime()==null){
            car.setInTime(time);
            iCarService.addTime(car);
        }else{
            car.setOutTime(time);
            iCarService.addTime(car);
        }
    }

    @PostMapping("/delete")
    public void delete(@RequestBody Car car){
        iCarService.delete(car);
    }

    @PostMapping("/update")
    public void update(@RequestBody Car car){
        iCarService.updateN(car);
    }

    @GetMapping("/all")
    public Object all(Integer page){
        if (page == null) {
            page = 1;
        }
        Car car = new Car();
        PageInfo<Car> pageInfo = iCarService.all(page,8);
        if(pageInfo == null){
            return ResponseResult.success();
        }else{
            return ResponseResult.success(pageInfo);
        }
    }

    @PostMapping("/findByCondition")
    public Object findByCondition(@RequestBody Car car){
        PageInfo<Car> pageInfo = iCarService.getPageByCondition(car,1,8);
        if(pageInfo == null){
            return ResponseResult.success();
        }else{
            return ResponseResult.success(pageInfo);
        }
    }
}
